export const textToImage = (name: string): string => {
  // 设置初始值,防止name为空时程序无法执行
  let nick = '未知'
  // 判断name是否为空
  if (name) {
    nick = name.charAt(0)
  }
  const fontSize = 14
  const fontWeight = 'normal'

  const canvas = document.createElement('canvas')
  canvas.width = 28
  canvas.height = 28
  const context = canvas.getContext('2d')
  if (!context) {
    return ''
  }
  // 头像背景颜色设置
  context.fillStyle = '#2D89EF'
  context.fillRect(0, 0, canvas.width, canvas.height)
  // 头像字体颜色设置
  context.fillStyle = '#FFFFFF'
  context.font = fontWeight + ' ' + fontSize + 'px sans-serif'
  context.textAlign = 'center'
  context.textBaseline = 'middle'
  context.fillText(nick, fontSize, fontSize)
  return canvas.toDataURL('image/png')
}
